package com.heze.xunhuan.controller;

import com.heze.xunhuan.entity.Category;
import com.heze.xunhuan.exception.BusinessException;
import com.heze.xunhuan.service.CategoryService;
import com.heze.xunhuan.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类控制器
 *
 * @author heze-xunhuan
 */
@Tag(name = "分类管理", description = "分类相关接口")
@RestController
@RequestMapping("/api/v1/categories")
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;

    @Operation(summary = "获取分类列表", description = "获取所有分类列表")
    @GetMapping
    public Result<List<Category>> getCategories() {
        List<Category> categories = categoryService.getAllCategories();
        return Result.success(categories);
    }

    @Operation(summary = "获取分类详情", description = "根据ID获取分类详情")
    @GetMapping("/{id}")
    public Result<Category> getCategory(@PathVariable Integer id) {
        Category category = categoryService.getCategoryById(id);
        if (category == null) {
            throw new BusinessException(404, "分类不存在");
        }
        return Result.success(category);
    }
}

